<?php
/*** 
 * @category		
 * @package		Lib			
 * @subpackage	Model	
 * @author		wanght <whtaok@gmail.com>
 * @since		2013-3-9
 * @version		$Id: CommonModel.class.php 22 2013-10-25 08:34:26Z whtaok@gmail.com $
 */

class CommonModel extends Model{
	
	public $modWhere= array();
	protected $_auto = array (
			array('is_valid','1',1), 	// 系统默认为1，新建
			array('is_show','1',1), 	// 系统默认为1，显示
			array('crt_time','time',1,'function'), // 对crt_time字段在更新的时候写入当前时间戳
			array('upd_time','time',3,'function'), // 对upd_time字段在更新的时候写入当前时间戳
			array('crt_user','getSUID',1,'callback'), // 对name字段在新增的时候回调getName方法
			array('upd_user','getSUID',3,'callback'), // 对name字段在新增的时候回调getName方法
	);
	
	public function getSUID(){
		$user_id= session(SES_CUST_ID);
		$shop_id= session(SES_SHOP_ID);
		return $shop_id."_".$user_id;
	}
	/**
	 * 描述：设置是否审核通过，默认显示全部状态，该状态由系统管理员审核
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $arg 例如：'1','1,2,3'
	 * @since		2013-4-21
	 */
	public function validForWhere($arg='1,2,3'){
		if($arg){
			$this->where(array('is_valid'=>array('in',$arg)));
		}
		return $this;
	}
	/**
	 * 描述：设置是否显示，默认显示全部状态，该状态有商户自己进行管理
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $arg 例如：0，1
	 * @since		2013-4-21
	 */
	public function showForWhere($arg=''){
		if($arg==='1' || $arg==='0'){
			$this->where(array('is_show'=>$arg));
		}
		return $this;
	}
	/**
	 * 描述：设置指定的ID
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $arg
	 * @since		2013-4-21
	 */
	public function idForWhere($arg = 0){
		$this->where(array('id'=>$arg));
		return $this;
	}
	
	/**
	 * 描述：更新指定的店铺ID
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $arg
	 * @since		2013-4-21
	 */
	public function shopIdWhere($arg=0){
		$this->where(array('shop_id'=>$arg));
		return this;
	}
	
	/**
	 * 描述：更新指定的管理者ID
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $arg
	 * @since		2013-4-21
	 */
	public function userIdWhere($arg=0){
		$this->where(array('user_id'=>$arg));
		return this;
	}
	/**
	 * 描述：指定查询条件
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $arg
	 * @since		2013-4-21
	 */
	public function setModWhere($arg = array(),$role='member'){
		foreach ($arg as $key=>$value ){
			if($value || $value=='0'){
				$this->modWhere[$key] = $value;
			}
		}
		if(!isset($this->modWhere['is_valid'])){
			if($role=='member'){
				$this->validForWhere(array(1,2));
			}else if($role=="master"){
				$this->validForWhere(array(1,2,3));
			}else if($role=='admin'){
				$this->validForWhere(array(1,2,3,4));
			}
		}
		return $this->where($this->modWhere);
	}
	
	public function defaultOrder(){
		return $this->order('ordlist');
	}
	
	public function listPage($totalRows=0){
		if(defined("IS_MOBILE") && IS_MOBILE){
		  	return $this->listPageMobile($totalRows);
		}else{ 
			return $this->listPagePc($totalRows);
		}
	}
	
	/**
	 * 描述：分页设置
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-5-1
	 */
	public function listPagePc($totalRows=0) {
		$nowPage = isset($_REQUEST['p'])?$_REQUEST['p']:1;
		import('ORG.Util.Page');
		//$totalRows = $this->setModWhere()->count();
		$listRows = 20;
		$pageObj = new Page($totalRows,$listRows);
		$list = $this->page($nowPage,$listRows)->select();
		$page = $pageObj->show();
		$rtn_data = array($list,$page);
		return $rtn_data;
	}
	
	/**
	 * 描述：分页设置
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-5-1
	 */
	public function listPageMobile($totalRows=0) {
		$nowPage = isset($_REQUEST['p'])?$_REQUEST['p']:1;
		import('ORG.Util.PageMobile');
		//$totalRows = $this->setModWhere()->count();
		$listRows = 10;
		$pageObj = new PageMobile($totalRows,$listRows);
		$list = $this->page($nowPage,$listRows)->select();
		$page = $pageObj->data();
		$rtn_data = array($list,$page);
		return $rtn_data;
	}
}

?>